package com.city.itis.dao.impl;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import com.city.itis.dao.UserDao;
import com.city.itis.domain.User;


/**
 * 用户Dao实现类
 * @author WY
 *
 */
@Component("userDao")
public class UserDaoImpl implements UserDao 
{
	
	private HibernateTemplate hibernateTemplate;
	public HibernateTemplate getHibernateTemplate() 
	{
		return hibernateTemplate;
	}
	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) 
	{
		this.hibernateTemplate = hibernateTemplate;
	}


	@Override
	public int save(User user) 
	{
		
		try
		{
			System.out.println("dao");
			int id=(Integer)this.hibernateTemplate.save(user);
			System.out.println(id);
			return id;
			
		}catch(Exception e)
		{
			//throw new AppException("系统出现错误，请联系管理员。");
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public int delete(User user) 
	{
		try 
		{
			this.hibernateTemplate.delete(user);
		} catch (Exception e) {
		return 0;
		}
		return 1;
	}

	@Override
	public int deleteById(Integer id) 
	{
		User user=(User) this.getHibernateTemplate().get(User.class, id);
		if(user!=null){
		try 
		{
			this.hibernateTemplate.delete(user);
		} catch (Exception e) {
			return 0;
		}
		return 1;
		}
		return 0;
	}

	@Override
	public int update(User user) 
	{
		try 
		{
			this.hibernateTemplate.update(user);
		} catch (Exception e) {
			return 0;
		}
		return 1;
	}

	@Override
	public User getUserById(Integer id) 
	{
		User user=(User)this.getHibernateTemplate().get(User.class, id);
	     if(user!=null)
	     {
	    	 return user;
	     }
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAllById(Integer id) {
		String queryString="FROM User where id!=? order by id desc  ";
		List<User> userList=(List<User>)this.hibernateTemplate.find(queryString,id);
		if(userList!=null)
		{
			return userList;
		}
		return null;
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAll() 
	{
		String queryString="FROM User order by id desc  ";
		List<User> userList=(List<User>)this.hibernateTemplate.find(queryString);
		if(userList!=null)
		{
			return userList;
		}
		return null;
	}
	
	@Override
	public int login(Integer id,String password)
	{
		User user=(User)this.getHibernateTemplate().get(User.class, id);
	     if(user!=null)
	     {
	    	 if(user.getPassword().equals(password))
	    	 {
	    		 return 1;
	    	 }
	     }
		return 0;
	}
	@SuppressWarnings("unchecked")
	@Override
	public User login(String userId, String password) 
	{
		String queryString="FROM User where userId=? and password=?";
		Object values[]={userId,password};
		List<User> userList=(List<User>)this.hibernateTemplate.find(queryString,values);
		if(userList!=null)
		{
			for(User user:userList)
			{
				return user;
			}
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	@Override
	public User getUserByUserId(String userId) 
	{
		String queryString="FROM User where userId=?";
		Object values[]={userId};
		List<User> userList=(List<User>)this.hibernateTemplate.find(queryString,values);
		if(userList!=null)
		{
			for(User user:userList)
			{
				return user;
			}
		}
		return null;
	}
	
}
